axios.defaults.baseURL = "http://123.57.109.30:3007";


//添加请求拦截器
axios.interceptors.request.use(function (config) {
    // 在发送请求之前做些什么
    if (config.url.startsWith("/my")) {
        config.headers['Authorization'] = sessionStorage.getItem("token");
    }

    return config;
}, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
});
//响应拦截器 
axios.interceptors.response.use(function (response) {
    // 对响应数据做点什么
    var layer = layui.layer;

    layer.msg(response.data.message, { id: "唯一" });
    //响应拦截器里 - 写上判断-防止错误的信息-进入到逻辑页面的成功的回调函数中
    if (response.data.status == 1) {
        return Promise.reject(response.data.message);//调用promise抛出错误到控制台中，阻止axios内部调用成功的回调函数
    }
    return response;
}, function (error) {
    // 对响应错误做点什么

    if (error.response.status == 403) {
        layer.msg(error.response.data.message, {
            icon: 2, end: function () {
                window.parent.location.href = "/login.html"

            }
        })

        return Promise.reject(error.response.data.message);
    }
    return Promise.reject(error);
});
const registerAPI = (arg, fn) =>
    axios({
        url: '/api/reguser',
        method: 'POST',
        data: arg,

    }).then(fn)



const loginAPI = (arg, fn) =>
    axios({
        url: '/api/login',
        method: 'POST',
        data: arg,
    }).then(fn)


const addCateAPI = (arg, fn) =>
    axios({
        url: '/my/article/addcates',
        method: 'POST',
        data: arg,


    }).then(fn)


const cateListAPI = (arg, fn) => axios({
    url: '/my/article/cates',
    method: 'GET',

}).then(fn)
//分类删除
const deiCateAPI = (arg, fn) => axios({
    url: '/my/article/deletecate/' + arg.id,
    method: 'GET',

}).then(fn)

const upCateAPI = (arg, fn) => axios({
    url: '/my/article/updatecate',
    method: 'POST',
    data: arg,

}).then(fn)


//个人中心 获取用户信息
const getUserInForAPI = (arg, fn) => axios({
    url: '/my/userinfo',
    method: 'GET',
}).then(fn)

const upUserInfoAPI = (arg, fn) => axios({
    url: '/my/userinfo',
    method: 'POST',
    data: arg,
}).then(fn)

const rePassAPI = (arg, fn) => axios({
    url: '/my/updatepwd',
    data: arg,
    method: 'POST',
}).then(fn)

const upAvatorAPI = (arg, fn) => axios({
    url: '/my/update/avatar',
    data: arg,
    method: 'POST',
}).then(fn)

const addArtAPI = (arg, fn) => axios({
    url: "/my/article/add",
    data: arg,
    method: 'POST'
}).then(fn)

const artListAPI = (arg, fn) => axios({
    url: "/my/article/list",
    params: arg,
    method: 'GET'
}).then(fn)



const getArgByIdAPI = (arg, fn) => axios({
    url: "/my/article/" + arg,

    method: 'GET'
}).then(fn)



const upArgByIdAPI = (arg, fn) => axios({
    url: "/my/article/edit",
    data: arg,
    method: 'POST'
}).then(fn)

const delArgAPI = (arg, fn) => axios({
    url: "/my/article/delete/" + arg,


    method: 'GET'
}).then(fn)



